package com.kuaimacode.kframework.admin.services.finance;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.admin.services.agent.SysAgentService;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.mybatis.mapper.finance.AgentDrawApplyMapper;
import com.kuaimacode.kframework.mybatis.models.agent.SysAgent;
import com.kuaimacode.kframework.mybatis.models.finance.AgentAccountDetail;
import com.kuaimacode.kframework.mybatis.models.finance.AgentDrawApply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author anzy
 * @date 2019/05/23
 */
@Service
public class  AgentDrawApplyService extends ServiceImpl<AgentDrawApplyMapper, AgentDrawApply> {

	@Autowired
	private SysAgentService sysAgentService;

	@Autowired
	private AgentAccountDetailService agentAccountDetailService;

	/**
	 *  分页查找所有的数据
	 * @param pageNum 当前页码，从1开始
	 * @param pageSize  一页显示多少行
	 * @return
	 */
	public PageInfo<AgentDrawApply> findAll(AgentDrawApply apply,int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<AgentDrawApply> datas = baseMapper.queryApplyList(apply);
		return new PageInfo<AgentDrawApply>(datas);
	}



	@Transactional
	public String passApply(String id){

		SysAgent sysAgent = sysAgentService.getById(Constant.DEFAULT_AGENT_ID);
		AgentDrawApply agentDrawApply = baseMapper.selectById(id);
		//插入平台 账户明细表
		AgentAccountDetail detail = new AgentAccountDetail();
		detail.setTranType(Constant.AgentAccountTranTypeEnum.FEE.getCode().toString());
		detail.setInOut(Constant.InOutEnum.IN.getCode().toString());
		detail.setAmount(agentDrawApply.getFee());
		detail.setTranDesc("分站["+agentDrawApply.getAgentId()+"]提现手续费收入");
		detail.setAgentId(Constant.DEFAULT_AGENT_ID);
		detail.setTranBal(sysAgent.getBalance().add(agentDrawApply.getFee()));
		detail.setTranId(agentDrawApply.getId().toString());
		agentAccountDetailService.insertAgentAccountDetail(detail);
		sysAgent.setBalance(sysAgent.getBalance().add(agentDrawApply.getFee()));

		sysAgentService.updateById(sysAgent);


    	return "success";

	}



	@Transactional
	public String cancleApply(String id,String reason){

		//判断提现申请的状态是否变更
		AgentDrawApply agentDrawApply = baseMapper.selectById(id);
		if(agentDrawApply.getStatus() == 1){
			//更新提现申请状态为取消
			UpdateWrapper updateWrapper = new UpdateWrapper();
			updateWrapper.eq("status",1);
			updateWrapper.eq("id",agentDrawApply.getId());
			agentDrawApply.setStatus(3);
			agentDrawApply.setReason(reason);
			int upCnt = baseMapper.update(agentDrawApply,updateWrapper);
			if(upCnt ==0){//更新失败
				return "该申请状态已经变更！";

			}
			//增加账户余额
			SysAgent agent = sysAgentService.getById(agentDrawApply.getAgentId());
			sysAgentService.updateBal(agentDrawApply.getAgentId(), Constant.InOutEnum.IN.getCode().toString(),agentDrawApply.getAmount());
			//插入交易明细
//			UpdateWrapper updateWrapper1 = new UpdateWrapper();
//			updateWrapper1.eq("tran_type",Constant.AgentAccountTranTypeEnum.DRAW.getCode());
//			updateWrapper1.eq("agent_id",agentDrawApply.getAgentId());
//			updateWrapper1.eq("tran_id",agentDrawApply.getId());
//			agentAccountDetailService.update(detail,updateWrapper1);
			AgentAccountDetail detail = new AgentAccountDetail();
			detail.setInOut("1");//收入
			detail.setTranType("4");//提现失败退款
			detail.setAmount(agentDrawApply.getAmount());//交易金额
			detail.setAgentId(agentDrawApply.getAgentId());//分站id
			detail.setTranId(agentDrawApply.getId().toString());
			detail.setTranBal(agent.getBalance().add(agentDrawApply.getAmount()));//交易后账户余额
			detail.setStatus(Constant.DeleteStateEnum.NORMAL.getCode().toString());
			detail.setTranTime(new Date());
			agentAccountDetailService.insertAgentAccountDetail(detail);


		}else{

			return "该申请状态已经变更！";
		}
		return "success";

	}

	/**
	 * 查询数量
	 * param status 0-申请 1-审批
	 * @return
	 */
	public int count(Integer status) {
		QueryWrapper<AgentDrawApply> wrapper = new QueryWrapper<>();
		wrapper.eq("status", status);
		return baseMapper.selectCount(wrapper);
	}

}

